var pageSize = 10;
var pageNum = 1;
var page;
var laypage;
var list;
var layer;


$(function () {
    $("#menuId").val(sessionStorage.menuId);
    layui.use(['laydate', 'element', 'laypage', 'layer'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入

        var start = {
            // min: laydate.now(),
            max: '2099-06-16 23:59:59'
            ,istoday: true
            ,choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            // min: laydate.now(),
            max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };

        document.getElementById('LAY_demorange_s').onclick = function(){
            start.elem = this;
            laydate(start);
        }
        document.getElementById('LAY_demorange_e').onclick = function(){
            end.elem = this
            laydate(end);
        }
    });
    setTimeout(function () {
        fundOrderList();
    }, 200);
});

//查询订单列表
function fundOrderList() {
    var menuId = $("#menuId").val();
    var queryKey = $("#queryKey").val();
    var startTime = $("#LAY_demorange_s").val();
    var endTime = $("#LAY_demorange_e").val();
    var state = $("#state").val();

    var params = {
        "type": "query",
        "menuId": menuId,
        "pageSize": pageSize,
        "pageNum": pageNum,
        "queryKey": queryKey,
        "startTime":startTime,
        "endTime":endTime,
        "state":state
    };
    $.ajax({
        type: "post",
        url: "/order/fundOrderList",
        data: params,
        dataType: "json",
        success: function (data) {
            if (data.code == 1 && data.code != undefined) {
                list = data.orderList;
                page = data.page;
                var html = "";
                for (var l in list) {
                    var createTime = '';
                    var finishTime = '';
                    var state = list[l].orderState;
                    if (state == 100) {
                        state = "未支付";
                    } else if (state == 200) {
                        state = "支付完成";
                    } else if (state == 300) {
                        state = "支付成功";
                    }
                    if (list[l].createTime != "" && list[l].createTime != null) {
                        createTime = new Date(list[l].createTime).Format("yyyy-MM-dd hh:mm:ss");
                    }
                    if (list[l].finshiTime != "" && list[l].finshiTime != null) {
                        finishTime = new Date(list[l].finshiTime).Format("yyyy-MM-dd hh:mm:ss");
                    }
                    html += '<tr><td>' + list[l].id + '</td>' +
                        '<td style="word-wrap:break-word;word-break:break-all;">' + list[l].orderNo + '</td>' +
                        '<td>' + list[l].title + '</td>' +
                        '<td>' + state + '</td>' +
                        '<td>' + list[l].dealPrice / 100 + '</td>' +
                        '<td>' + createTime + '</td>' +
                        '<td>' + finishTime + '</td>' +
                        // '<td>'+list[b].hobby+'</td>'+
                        // '<td>' + new Date(list[l].createTime).toLocaleString() + '</td>' +
                        '<td><button class="layui-btn layui-btn-normal" onclick="findDetails('+l+')">查看详情</button></td></tr>'
                }
                $("#orderList").html(html);
                $("#total").html("共有数据：" + page.total + " 条");
                laypage({
                    cont: 'page'
                    , pages: page.pages
                    , first: 1
                    , curr: pageNum
                    , last: page.pages
                    , prev: '<em><</em>'
                    , next: '<em>></em>'
                    , jump: function (obj, first) {
                        pageNum = obj.curr;
                        if (!first) {
                            fundOrderList();
                        }
                    }
                });

            } else if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
            }else if(dsta.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
            }
        }
    });
}

//查询按钮
function search() {
    pageNum = 1;
    fundOrderList();
}
//订单详情
function findDetails(l) {

    var order = list[l];
    var state = "";
    if (order.orderState == 100) {
        state = "未支付";
    } else if (order.orderState == 200) {
        state = "支付完成";
    } else if (order.orderState == 300) {
        state = "支付成功";
    }
    var orderType = "";
    if(order.orderType == 1){
        orderType = "广告币充值";
    }else if(order.orderType == 2){
        orderType = "创客合伙人充值";
    }else if(order.orderType == 3){
        orderType = "消费金充值";
    }else if(order.orderType == 4){
        orderType = "优惠买单";
    }
    var paySource = "";
    if(order.paySource == 1){
        paySource = "Ios";
    }else if(order.paySource == 2){
        paySource = "Android";
    }else if(order.paySource == 3){
        paySource = "weixin";
    }else if(order.paySource == 4){
        paySource = "web";
    }
    var html = '<table style="width: 100%;border-collapse: separate;border-spacing: 10px;margin-top: 10px;margin-bottom:100px;">'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">用户ID:</td><td style="text-align: left">'+order.userId+'</td>'+
        '<td style="text-align: right;font-size: 18px;font-weight: 600">昵称:</td><td style="text-align: left">'+order.nickName+'</td></tr>'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">手机号码:</td><td style="text-align: left">'+order.phone+'</td>'+
        '<td style="text-align: right;font-size: 18px;font-weight: 600">性别:</td><td style="text-align: left">'+order.sex+'</td></tr>'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">订单类型:</td><td style="text-align: left">'+orderType+'</td>'+
        '<td style="text-align: right;font-size: 18px;font-weight: 600">订单状态:</td><td style="text-align: left">'+state+'</td></tr>'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">订单流水号:</td><td style="text-align: left">'+order.orderNo+'</td>'+
        '<td style="text-align: right;font-size: 18px;font-weight: 600">支付流水号:</td><td style="text-align: left">'+order.payNo+'</td></tr>'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">订单来源:</td><td style="text-align: left">'+paySource+'</td>'+
        '<td style="text-align: right;font-size: 18px;font-weight: 600">下单时间:</td><td style="text-align: left">'+(order.createTime == null ? "" : new Date(order.createTime).toLocaleString())+'</td></tr>'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">完成时间:</td><td style="text-align: left">'+(order.finshiTime == null ? "" : new Date(order.finshiTime).toLocaleString())+'</td>'+
        '<td style="text-align: right;font-size: 18px;font-weight: 600">修改时间:</td><td style="text-align: left">'+(order.modifyTime == null ? "" : new Date(order.modifyTime).toLocaleString())+'</td></tr>'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">原始金额:</td><td style="text-align: left">'+order.originalPirce / 100+'</td>'+
        '<td style="text-align: right;font-size: 18px;font-weight: 600">成交金额:</td><td style="text-align: left">'+order.dealPrice / 100+'</td></tr>'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">折扣金额:</td><td style="text-align: left">'+order.discountPrice / 100+'</td>'+
        '<td style="text-align: right;font-size: 18px;font-weight: 600">优惠券抵扣金额:</td><td style="text-align: left">'+order.cashCouponDeductPrice / 100+'</td></tr>'+
        '<tr><td style="text-align: right;font-size: 18px;font-weight: 600">积分抵扣金额:</td><td style="text-align: left">'+order.integrationDeductPrice / 100+'</td></tr></table>';

    layer.open({
        title: ['订单详情','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['60%', '60%']
        ,maxmin:true
    });

}